<?php
/**
 * Created by PhpStorm.
 * User: dx
 * Date: 16/2/19
 * Time: 下午4:20
 */
class ActivityBaseInfo extends Eloquent
{
    protected $table = "activity_base_info";

    /*
     * 发放方式 1-线上 2-线下
     * */
    public static $product_type = array(
        "1"=>"线上",
        "2"=>"线下",
        "3"=>"全部",
    );
    /*
     * 状态 1-启用 0-停用
     * */
    public static $status = array(
        "1"=>"启用",
        "0"=>"停用",
    );

    public static $types = array(
        "1"=>"抽奖活动",
        "0"=>"普通活动",
    );

    public function activity_coupon_info()
    {
        return $this->hasOne('ActivityCouponInfo',"activity_id","id");
    }

    public function coupon_base_info()
    {
        return $this->hasOne('CouponBaseInfo',"id","coupon_base_info_id");
    }

    /**
     * @Author:  kc
     * @DateTime:    2017-12-15 15:36:00
     * @Param 传入一个活动
     * @Description: 检查抽奖活动的的 有效性  是否抽奖活动  时间不判断
     */
    public static function checkAward($activity,&$response){
        if(is_null($activity) || $activity->type == 0){
            $response = Lang::get("error.activity_not_exist");
            return NULL;
        }
        if($activity->status==0){
            $response = Lang::get("error.activity_status_error_51001");
            return NULL;
        }
        $time=time();
        if(strtotime($activity->begin_date) > $time){
            $response = Lang::get("error.activity_not_start_error_51011");
            return NULL;
         }
         if(strtotime($activity->end_date) < $time){
             $response = Lang::get("error.activity_game_over_error_51002");
             return NULL;
         }
        return $activity;
    }
    /**
     * @Author:  kc
     * @DateTime:    2017-12-15 15:36:00
     * @Param 传入一个活动
     * @Description: 检查活动的的 有效性  是否抽奖活动  时间不判断
     */
    public static function checkActivity($activity,&$response){
        if(is_null($activity)){
            $response = Lang::get("error.activity_not_exist");
            return NULL;
        }
        if($activity->status==0){
            $response = Lang::get("error.activity_status_error_51001");
            return NULL;
        }
        $time=time();
        if(strtotime($activity->begin_date) > $time){
            $response = Lang::get("error.activity_not_start_error_51011");
            return NULL;
        }
        if(strtotime($activity->end_date) < $time){
            $response = Lang::get("error.activity_game_over_error_51002");
            return NULL;
        }
        return $activity;
    }
    /**
     * @Author:      kc
     * @DateTime:    2017-12-16 11:43:51
     * @Description: 是否能参加 某个抽奖活动
     * @param 订单信息  一个抽奖活动
     * 不验证 抽奖活动的有效性  只验证订单是否能参加活动
     * 验证订单状态 验证订单时间
     */
    public static function isCanUserActivity($order,$activity_award,&$response){
        if(is_null($order)||!in_array($order->status, Orders::$complete)){
            $response = Lang::get("error.activity_award_order_error_51016");
            return NULL;
        }
        if(strtotime($activity_award->begin_date)>strtotime($order->pay_time)||strtotime($activity_award->end_date)<strtotime($order->pay_time)){
            $response = Lang::get("error.activity_award_order_time_error_51017");
            return NULL;
        }
        return $order;
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-05-25 10:43:51
     * @Description: 活动是否存在
     */
    public static function getActivityBaseInfo($activity_id){
        return DB::table('activity_base_info')->where('id',$activity_id)->where('status','1')->first();
    }
}